from autoTest_brtcApi.core.usercenter_apiUtils import userApiUtil
from autoTest_brtcApi.utils.write import write_yaml
from autoTest_brtcApi.utils.read import readFile
###在该模块的方法里面构造请求头和参数
headers = {"Accept": "application/json",
           "Content-Type": "application/json"
           }
class UserApi():
   #注册
    def reg_api(self,username,password,phone):
        params = {"user_name": username,
                  "passwd": password,
                  "phone": phone
                  }
        # 接受接口返回的数据
        res =userApiUtil.reg_util(headers=headers,json=params)
        return res

    #登录
    def login_api(self,password,phone):
        params = { "passwd":password,
                    "phone": phone
                  }
        # 接受接口返回的数据
        res_login =userApiUtil.login_util(headers=headers,json=params)
        if res_login.json()['code']==0:
            # 获取userId和token值
            userId = res_login.json()["data"]["userId"]
            token = res_login.json()["data"]["token"]
            ##把动态变化的userId和token放在yaml文件中
            content = {"userId": str(userId),
                       "token": token}
            write_yaml("tmp.yaml", content)
        return res_login

    def modifyUserNick(self):
        #读取tmp.yaml,获取userId和token
        response=readFile("tmp.yaml")
        headers_modifyUserNick = {"userId": str(response["userId"]),
                                  "token":response["token"]
                                  }

        params = {"nickName": "jhyx1"}

        res=userApiUtil.modifyUserNick_util(headers_modifyUserNick,data=params)
        return res
#实例化
userApi=UserApi()